package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Course;
import com.itheima.utils.BeanUtil;
import com.itheima.web.BaseServlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/catalog")
public class CatalogServlet extends BaseServlet {




    /**
     * 分页查询
     *
     * @param request
     * @param response
     * @return
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        PageInfo<Catalog> info = catalogService.findAll(page, size);
        request.setAttribute("page", info);
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/list.jsp").forward(request, response);
    }

    /**
     * 跳转到添加界面和用户科目数据回显的方法
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //添加之前需要将学科的信息回显到选择下拉框
        List<Course> list = courseService.findAll();
        request.setAttribute("courseList", list);
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/add.jsp").forward(request, response);
    }

    /**
     * 添加信息的方法
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        System.out.println(catalog);
        catalogService.save(catalog);
        response.sendRedirect(request.getContextPath() + "/store/catalog?operation=list");
    }



    /**
     * 跳转到修改界面,并回显数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //加载需要修改的数据信息
        String id = request.getParameter("id");
        Catalog catalog = catalogService.findById(id);
        request.setAttribute("catalog",catalog);
        //学科的数据回显
        List<Course> all = courseService.findAll();
        request.setAttribute("courseList",all);
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/update.jsp").forward(request,response);
    }

    /**
     * 修改数据
     * @param request
     * @param response
     * @throws IOException
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        catalogService.update(catalog);
        response.sendRedirect(request.getContextPath()+"/store/catalog?operation=list");
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        catalogService.delete(id);
        response.sendRedirect(request.getContextPath()+"/store/catalog?operation=list");
    }
}
